import { connect } from 'umi';
import styles from './index.less';
import Header from './components/Header';
import { queryMarketList } from './services';
import Main from './components/Main';
import { PageLoading, TRDefault } from '@/components/index.js';
import { Spin } from 'antd';
import React, { useEffect } from 'react';
import { useSetState } from 'ahooks';
import { getTreeByCodes } from '@/utils/index.js';
import { HttpCode } from '#/utils/contacts';

const TenDaysCentralizedBidding = ({ loadingNum, showMain, hasModified, dispatch, menuCode }) => {
  const [state, setState] = useSetState({
    error: '',
    power: [],
    loading: true,
    market: {},
  });

  // 刷新页面后，必须点击下当前页面再点击刷新按钮才会触发监听时间
  window.addEventListener('beforeunload', (e) => {
    if (hasModified) {
      let confirmationMessage = '当前工作将不被保存，继续执行此操作？';
      (e || window.event).returnValue = confirmationMessage;
      return confirmationMessage;
    }
  });

  const init = async () => {
    setState({ loading: true });
    let error = '';
    let power = [];
    const res = await queryMarketList(menuCode);
    if (res?.statusCode === HttpCode.SUCCESS) {
      const { treeOptions } = getTreeByCodes(['market', 'group', 'station'], res.json);
      power = treeOptions;
    } else {
      error = res?.message;
    }
    setState({
      loading: false,
      power,
      error: error,
    });
  };

  useEffect(() => {
    (async () => {
      // 获取交易场站列表
      await init();
    })();
    // dispatch({
    //   type: 'tenDaysCentralizedBidding/getStationLevelThreeEffect',
    // });
    dispatch({
      type: 'tenDaysCentralizedBidding/fetchTradeTimeEffect',
    });
  }, []);

  if (state.loading) return <PageLoading />;
  if (state.error) return <TRDefault type="error" message={state.error} />;
  if (!state.power.length) return <TRDefault type="lock" />;
  return (
    <Spin spinning={loadingNum !== 0}>
      <div className={styles.container}>
        <Header power={state.power} market={state.market} setFatherState={setState} />
        {showMain ? <Main market={state.market} /> : <TRDefault type="empty" />}
      </div>
    </Spin>
  );
};

export default connect(({ tenDaysCentralizedBidding, global }) => ({
  menuCode: global?.configure?.menuCode,
  currentProgramId: tenDaysCentralizedBidding.currentProgramId,
  loadingNum: tenDaysCentralizedBidding.loadingNum,
  showMain: tenDaysCentralizedBidding.showMain,
  hasModified: tenDaysCentralizedBidding.hasModified,
}))(TenDaysCentralizedBidding);

/**
 * 删除、编辑、定时权限重新梳理
 * deletePermission判断是否有删除权限
 * editPermission判断是否有编辑权限，现在删除编辑数据一致
 * programInfo.status判断方案状态 0：一般状态、1：申报锁定(锁定中 申报中)、2：已申报
 *
 * 显示删除按钮：deletePermission === true &&  programInfo.status === 0
 * table、保存、立即申报不可操作：editPermission === false || programInfo.status === 1 || programInfo.status === 2
 * 立即申报按钮不可操作：editPermission === false || programInfo.status === 2 || (programInfo.status === 1 && !programInfo.timelySubmitTime)
 * 立即申报按钮显示时间：editPermission === true &&  programInfo.status === 1 && programInfo.timelySubmitTime
 */

/**
 * 方案有没有被修改过的逻辑判断
 *
 * 方案被修改过的操作：1.修改交易单元 2.修改是否接受限额调剂 3.修改方案内容 4.新增方案
 * 方案未被修改过的操作：1.初次获取方案内容不算修改范围内 2.方案保存、申报、定时等操作成功后，设置为未被修改过
 *
 * 离开页面弹框触发情况：切换方案、删除当前方案、点击查询、刷新/关闭浏览器
 */
